package com.chebaohui.iqs.service.impl.deal.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * 时间工具基础类
 * 
 * @author regas_xie
 *
 */
public class TimeBase {

	/**
	 * 默认时间格式
	 */
	public static final String time_default = "yyyy-MM-dd HH:mm:ss";

	/**
	 * 保险公司接口返回时间格式
	 */
	public static final String company_time = "yyyy-MM-dd HH";

	/**
	 * 时间格式:yyyyMMddHHmmss
	 */
	public static final String time_sdf = "yyyyMMddHHmmss";

	/**
	 * 获取距今天相隔的天数
	 * 
	 * @param regax
	 *            获取时间格式
	 * @param day
	 *            距今天相隔的天数
	 * @return
	 * @throws Exception
	 */
	public static String getDay(String regax, int day) throws Exception {
		try {
			SimpleDateFormat sdf = new SimpleDateFormat(regax);
			Calendar calendar = Calendar.getInstance();
			calendar.setTime(new Date());
			calendar.add(Calendar.DATE, day);
			return sdf.format(calendar.getTime());
		} catch (Exception e) {
			throw new Exception("时间获取错误:" + e.getMessage());
		}
	}

	/**
	 * @param regax
	 * @param time
	 * @param day
	 * @return
	 * @throws Exception
	 */
	public static long getTime(String regax, String time, int day) throws Exception {
		SimpleDateFormat sdf = new SimpleDateFormat(regax);
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(sdf.parse(time));
		calendar.add(Calendar.DATE, day);
		return calendar.getTime().getTime();
	}

	/**
	 * 判断时间格式
	 * 
	 * @param regax
	 * @param time
	 * @return
	 * @throws Exception
	 */
	public static boolean dealTime(String regax, String time) throws Exception {
		SimpleDateFormat sdf = new SimpleDateFormat(regax);
		Date date = sdf.parse(time);
		return sdf.format(date).equals(time);
	}

	/**
	 * 时间格式转化
	 * 
	 * @param time
	 * @param timeSdf
	 * @param resSdf
	 * @return
	 * @throws Exception
	 */
	public static String timeToTime(String time, String timeSdf, String resSdf) throws Exception {
		return new SimpleDateFormat(resSdf).format(new SimpleDateFormat(timeSdf).parse(time));
	}

	/**
	 * 获取距给定时间相隔的天数
	 * 
	 * @param regax
	 *            获取时间格式
	 * @param day
	 *            距今天相隔的天数
	 * @return
	 * @throws Exception
	 */
	public static String getDay(String time, String regax, int day) throws Exception {
		try {
			SimpleDateFormat sdf = new SimpleDateFormat(regax);
			Calendar calendar = Calendar.getInstance();
			calendar.setTime(sdf.parse(time));
			calendar.add(Calendar.DATE, day);
			return sdf.format(calendar.getTime());
		} catch (Exception e) {
			throw new Exception("时间-获取错误:" + e.getMessage());
		}
	}

	/**
	 * 取下一年前一秒
	 * 
	 * @param time
	 *            时间（格式yyyy-MM-dd HH:mm:ss）
	 * @param regax
	 *            返回时间格式
	 * @return
	 * @throws Exception
	 */
	public static String getNextYear(String time, String regax) throws Exception {
		try {
			Calendar calendar = Calendar.getInstance();
			calendar.setTime(new SimpleDateFormat(time_default).parse(time));

			calendar.add(Calendar.YEAR, 1);
			calendar.add(Calendar.SECOND, -1);
			return new SimpleDateFormat(regax).format(calendar.getTime());
		} catch (Exception e) {
			throw new Exception("获取下一年时间错误:" + e.getMessage());
		}
	}
	/**
	 * 取投保日期的第二天作为起保时间
	 * @return
	 * @throws Exception 
	 */
	public static String getDefaultTime() throws Exception{
		try {
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
			Date today=new Date();
			String format = sdf.format(today);
			long ss = sdf.parse(format).getTime()+(1000*60*60*24);
			return (ss/1000+"");
		} catch (Exception e) {
			throw new Exception("获取下天错误:" + e.getMessage());
		}
	}
	/**
	 * 判断今日查询交强险是否在续保期内
	 * @return
	 * @throws Exception 
	 * @throws ParseException 
	 */
	public static boolean isForce(String into) throws Exception {
		boolean isnull=false;
		try {
			SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			long time = sdf.parse(into).getTime();
			long time2 = new Date().getTime();
			isnull=time>time2;
			return isnull;		
		} catch (Exception e) {
			throw new Exception("时间格式错误");
		}
	}
	
 	
	
}
